# -*- coding: utf-8 -*-
# @Time    : 2024/6/24 9:58
# @Author  : floraachy
# @File    : repo_copy_issue_page
# @Software: PyCharm
# @Desc:

# 标准库导入
import os
from typing import Optional, Union, List
# 第三方库导入
import allure
# 本地应用/模块导入
from utils.base_utils.base_page import BasePage


class RepoCopyIssuePage(BasePage):
    """项目-复制疑修页面"""
    # 疑修标题
    locator_issue_title_input = "id=subject"
    # 疑修内容
    locator_issue_content_input = "//textarea/following::div[contains(@class, 'CodeMirror-wrap')]"
    # 疑修附件
    locator_issue_file_input = "//p[text()='拖动文件或点击此处上传']/parent::div/preceding-sibling::input"
    # 保存 按钮
    locator_save_btn = "//span[contains(text(), '保 存')]/.."

    @allure.step("访问复制疑修页面：/{repo_owner}/{repo_identifier}/issues/{issue_index}/copy")
    def navigate(self, repo_owner, repo_identifier, issue_index):
        """
        访问复制疑修页面
        """
        self.visit(f"/{repo_owner}/{repo_identifier}/issues/{issue_index}/copy")

    @allure.step("输入疑修标题: {issue_title}")
    def input_issue_title(self, issue_title: str):
        """
        输入疑修标题
        """
        self.page.wait_for_timeout(300)  # 超时时间是0.3秒
        self.input(locator=self.locator_issue_title_input, text=issue_title)

    @allure.step("输入疑修内容: {issue_desc}")
    def input_issue_desc(self, issue_desc: str):
        """
        输入疑修内容
        """
        # todo 疑惑：这里必须加wait_for_timeout，否则即使用的wait_for_selector，也无法输入内容。
        self.page.wait_for_timeout(2000)
        # elem = page.wait_for_selector(
        #     selector="//textarea/following::div[contains(@class, 'CodeMirror-wrap')]", timeout=10000, state='visible')
        elem = self.page.query_selector(selector=self.locator_issue_content_input)
        self.execute_js('''([elem, content]) => elem.CodeMirror.setValue(content);''',
                        [elem, issue_desc])

    @allure.step("上传疑修附件: {issue_file}")
    def upload_issue_attachments(self, issue_file: str):
        """
        上传疑修附件
        """
        self.upload_file(locator=self.locator_issue_file_input, file_path=issue_file)

    @allure.step("点击【保存】按钮，提交表单校验，进行疑修更新")
    def click_save_issue_button(self):
        """
        点击'保存'按钮，提交表单校验，进行疑修更新
        """
        self.click(self.locator_save_btn)
        self.page.wait_for_timeout(3000)

    # --------------------- 流程 -------------------------------------
    @allure.step("复制疑修流程，标题：{issue_title}， 内容：{issue_desc}， 附件：{issue_file}")
    def copy_issue_flow(self, issue_title: str, issue_desc: Optional[str] = None,
                        issue_file: Optional[Union[str, List[str]]] = None) -> None:
        """
        疑修详情页，复制疑修流程:
        1. 点击复制按钮
        2. 输入疑修标题(必填)
        3. 输入疑修内容(选填)
        4. 上传疑修附件(选填)
       5. 点击"保存"按钮
        """
        self.input_issue_title(issue_title)
        if issue_desc:
            self.input_issue_desc(issue_desc)
        if issue_file:
            if isinstance(issue_file, list):
                for file in issue_file:
                    self.upload_issue_attachments(file)
        if isinstance(issue_file, str):
            self.upload_issue_attachments(issue_file)
        self.click_save_issue_button()
